<template>
  <div>
    <div>
      <headerTop></headerTop>
    </div>
    <div>
      <headerMin></headerMin>
    </div>
    <div class="index_content">
      <div class="banner">
        <el-carousel trigger="click" height="512px">
          <el-carousel-item v-for="(v,i) in banner_List" :key="i">
            <img :src="v.image" alt />
          </el-carousel-item>
        </el-carousel>
      </div>
      <div class="wrap">
        <!-- nav 条 -->
        <div class="nav">
          <div class="nav1">
            <router-link to="/doctor/index">
              <h1>线上问诊</h1>
              <h2>24小时在线咨询</h2>
              <img src="../../assets/image/index02.png" alt />
              <p>免费问诊</p>
            </router-link>
          </div>
          <div class="nav2">
            <router-link to="/shenfang/index">
              <h1>药师审方</h1>
              <h2>处方在线审核</h2>
              <img src="../../assets/image/index03.png" alt />
              <p>立即审方</p>
            </router-link>
          </div>
          <div class="nav3">
            <router-link to="/pharmacist/index">
              <h1>药师咨询</h1>
              <h2>在线用药指导</h2>
              <img src="../../assets/image/index04.png" alt />
              <p>立即咨询</p>
            </router-link>
          </div>
          <div class="nav4">
            <router-link to="/expert/index">
              <h1>找专家</h1>
              <h2>专家资深问诊</h2>
              <img src="../../assets/image/index05.png" alt />
              <p>在线问诊</p>
            </router-link>
          </div>
          <div class="nav5">
            <router-link to="/fast">
              <h1>快速购药</h1>
              <h2>历史用药快速购买</h2>
              <img src="../../assets/image/index01.png" alt />
              <p>立即购买</p>
            </router-link>
          </div>
        </div>
        <!-- 活动 促销 -->
        <div class="promotion">
          <div class="all">
            <router-link to="./promotion">查看更多>></router-link>
          </div>
          <div class="goods_titles">
            <i></i>
            <span>活动促销</span>
          </div>
          <div class="goods">
            <div @click="GoDetails(v.id)" v-for="(v,i) in activity_List" :key="i">
              <img :src="v.goods_img" alt />
              <p class="one">{{v.goods_name}}</p>
              <p class="money">
                ￥{{v.price}}
                <span>￥{{v.market_price}}</span>
              </p>
            </div>
          </div>
          <div class="recommend">
            <div class="goods_titles">
              <i></i>
              <span>推荐产品</span>
            </div>
            <div class="navs">
              <div
                @click="recommend_Tab(v,i)"
                :class="{ recommend_Active : recommendNavActive == i}"
                v-for="(v,i) in recommendNav"
                :key="i"
              >
                {{v.category_name}}
                <i></i>
                <span></span>
              </div>
              <div @click="GoAllGoods()">
                全部商品>>
                <i></i>
              </div>
            </div>
            <div class="goods">
              <div @click="GoDetails(v.id)" v-for="(v,i) in  recommend_List" :key="i">
                <div class="goodsImg">
                  <img :src="v.goods_img" alt />
                </div>
                <p class="one">￥ {{v.market_price}}</p>
                <p class="two">{{v.goods_name}}</p>
                <div class="bottom">
                  <p>
                    评论
                    <span>{{v.commentSum}}</span>条
                  </p>
                  <button>立即购买</button>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
    <div class="user_Top_Cart">
      <div
        @mouseover="userState = 1"
        @mouseout="userState = 0"
        :class="{ bg_cart:$user.state == 1}"
        class="cart"
      >
        <img v-if="$user.state == 0" src="@/assets/image/right_ShoppingCart_icon_gray@2x.png" alt />
        <img v-if="$user.state == 1" src="@/assets/image/right_ShoppingCart_icon_white@2x.png" alt />
        <br />购物车
      </div>
      <div v-if="btnFlag" @click="backTop" class="Top">
        <img src="@/assets/image/right_TOP_icon@2x.png" alt />
      </div>
    </div>
    <div
      @mouseover="userState = 1"
      @mouseout="userState = 0"
      v-show="userState == 1"
      class="cart_box"
    >
      <div class="cart_boxs">
        <div v-if="$user.state ==0" class="card_none">
          您未登录 请
          <span>登录</span>
        </div>
        <div v-if="$user.state ==1">
          <div v-for="(v,i) in cartList" :key="i" class="cart_list">
            <div class="fl imgs">
              <img :src="v.goods_img" alt />
            </div>
            <div class="fl txt">
              <p class="one">{{v.goods_name}}</p>
              <p class="one">{{v.price}} 元</p>
            </div>
            <div class="fr">
              ￥12.00
              <span @click="cartDelt(v)">删除</span>
            </div>
          </div>
        </div>
      </div>
      <button @click="Gocart()">查看我的购物车</button>
    </div>
    <div class="bg1">
      <publicBottom></publicBottom>
    </div>
  </div>
</template>
<script>
import headerTop from "@/components/public/public_headerTop.vue";
import headerMin from "@/components/index/index_header_min";
// import landChecking from "@/components/land-checking.vue";
import publicBottom from "@/components/public/public_bottom.vue";
import { getAllCate } from "@/request/index"; //  首页获取分类
import { top_index } from "@/request/index"; //  首页获取推荐药品
import { banner } from "@/request/index"; //  首页获取 轮播图
import { getActiveGoods } from "@/request/index"; //  活动
import { Goods_cart_index } from "@/request/index"; //查看当前门店下的购物车
import { delCart } from "@/request/index"; // 购物车 删除 商品
export default {
  components: {
    headerTop,
    headerMin,
    publicBottom
  },
  props: [],
  data() {
    return {
      search: "",
      recommendNav: [], //推荐 NAV
      recommendNavActive: 0, // 推荐 active
      recommend_List: [], //推荐列表
      banner_List: [], //轮播图
      activity_List: [], //活动 列表
      userState: 0, //显示隐藏 购物车
      btnFlag: false, // 显示隐藏 回到顶部
      cartList: [] //购物车列表
    };
  },
  created() {
    this.start();
  },
  mounted() {
    window.addEventListener("scroll", this.scrollToTop);
    if (this.$user.state == 1) {
      this.init();
    }
  },
  destroyed() {
    window.removeEventListener("scroll", this.scrollToTop);
  },
  methods: {
    Gocart() {
      if (this.$user.state == 0) {
        this.$message.error("请登录后 查看");
      } else {
        this.$router.push({ path: "/cart" });
      }
    },
    // 购物车删除商品
    cartDelt(e) {
      delCart({
        token: this.$user.token,
        CartId: e.id
      }).then(res => {
        if (res.code == 1) {
          this.$message({
            message: "删除成功",
            type: "success"
          });
          this.cartList = [];
          this.init();
        }
      });
    },
    //  购物车加载
    init() {
      let shop_id = localStorage.getItem("lzyyShopId");
      Goods_cart_index({
        token: this.$user.token,
        shop_id: shop_id
      }).then(res => {
        let data = res.data.datas;
        this.cartList = data;
      });
    },
    // 点击图片回到顶部方法，加计时器是为了过渡顺滑
    backTop() {
      const that = this;
      let timer = setInterval(() => {
        let ispeed = Math.floor(-that.scrollTop / 5);
        document.documentElement.scrollTop = document.body.scrollTop =
          that.scrollTop + ispeed;
        if (that.scrollTop === 0) {
          clearInterval(timer);
        }
      }, 16);
    },
    // 为了计算距离顶部的高度，当高度大于60显示回顶部图标，小于60则隐藏
    scrollToTop() {
      const that = this;
      let scrollTop =
        window.pageYOffset ||
        document.documentElement.scrollTop ||
        document.body.scrollTop;
      that.scrollTop = scrollTop;
      if (that.scrollTop > 60) {
        that.btnFlag = true;
      } else {
        that.btnFlag = false;
      }
    },
    GoDetails(id) {
      this.$router.push({ path: "goodsDetails?id=" + id });
    },
    recommend_Tab(v, i) {
      let shop_id = localStorage.getItem("lzyyShopId");
      this.recommendNavActive = i;
      top_index({
        category_id: v.id,
        shop_id: shop_id
      }).then(res => {
        this.recommend_List = res.data.data;
      });
    },
    start() {
      let shopId = localStorage.getItem("lzyyShopId");
      //  轮播图
      banner({
        position_id: 2
      }).then(res => {
        this.banner_List = res.data;
      });
      //  活动
      getActiveGoods({
        shop_id: shopId
      }).then(res => {
        this.activity_List = res.data.data;
      });
      // 分类 推荐
      getAllCate().then(res => {
        let shop_id = localStorage.getItem("lzyyShopId");
        let datas = res.data;
        this.recommendNav = res.data;
        var arr = [];
        for (let i in datas) {
          arr.push(datas[i]); //属性
          //arr.push(obj[i]); //值
        }
        let array = [];
        for (let j = 0; j < 6; j++) {
          array.push(arr[j]); //属性
        }
        this.recommendNav = array;

        top_index({
          shop_id: shop_id,
          category_id: this.recommendNav[0].id
        }).then(res => {
          this.recommend_List = res.data.data;
        });
      });
    },
    GoAllGoods() {
      this.$router.push({ path: "/allGoods" });
    }
  }
};
</script>
<style lang="less" scoped>
.card_none {
  margin-top: 75px;
  text-align: center;
  font-size: 16px;
  span {
    display: inline-block !important;
    color: #38eec1 !important;
  }
}
.cart_box {
  position: fixed;
  bottom: 16px;
  right: 58px;
  z-index: 9;
  width: 342px;
  height: 240px;
  background: #f7f7f7;
  border: 1px solid rgba(56, 238, 193, 1);
  text-align: right;

  img {
    width: 44px;
    height: 44px;
    margin-right: 6px;
  }
  button {
    margin-top: 10px;
    margin-right: 10px;
    width: 132px;
    height: 28px;
    background: rgba(56, 238, 193, 1);
    font-size: 16px;
  }
  .txt {
    width: 160px;
  }
  span {
    display: block;
    color: #cccccc;
  }
  .fr {
    text-align: right;
  }
  .cart_boxs {
    height: 193px;
    overflow-y: auto;
    border-bottom: 1px solid #000000;
    background: white;
  }
  .cart_list {
    height: 64px;
    border-bottom: 1px solid #000000;
    padding-top: 10px;
    padding-left: 10px;
    padding-right: 10px;
    overflow: hidden;
  }
}
.bg_cart {
  background: rgba(56, 238, 193, 1) !important;
}
.user_Top_Cart {
  position: fixed;
  bottom: 120px;
  height: 136px;
  width: 58px;
  right: 0;
  z-index: 9;
  .cart {
    width: 58px;
    text-align: center;
    height: 78px;
    background: #e6e6e6;
    padding-top: 14px;
    font-size: 13px;
    color: #ffffff;
    img {
      width: 30px;
      height: 27px;
      margin-bottom: 4px;
    }
  }
  .Top {
    width: 58px;
    height: 58px;
    text-align: center;
    padding-top: 14px;
    background: rgba(102, 102, 102, 1);
    img {
      width: 28px;
      height: 30px;
    }
  }
}
// index
.index_content {
  .banner {
    height: 512px;
    min-width: 1200px;

    img {
      height: 512px;
      min-width: 1200px;
    }
  }

  .nav {
    margin-top: 40px;
    display: flex;
    justify-content: space-between;

    > div {
      border-radius: 8px;
      width: 232px;
      height: 255px;
      text-align: center;
      color: white;
    }

    h1 {
      margin-top: 20px;
      font-size: 30px;
      color: white;
      font-weight: 400;
    }

    h2 {
      font-size: 24px;
      margin-top: 12px;
      margin-bottom: 18px;
      color: white;
      font-weight: 400;
    }

    img {
      width: 62px;
      height: 78px;
      margin-bottom: 10px;
      color: white;
    }

    p {
      margin: auto;
      font-size: 22px;
      width: 124px;
      height: 34px;
      line-height: 34px;
      color: white;
      border-radius: 16px;
    }
  }

  .nav1 {
    background: linear-gradient(to right, #bbdfff, #97cdf9);

    p {
      background: linear-gradient(to right, #6b9efd, #4b7bd4);
    }
  }

  .nav2 {
    background: linear-gradient(to right, #68daff, #27b8ee);

    p {
      background: linear-gradient(to right, #4eaeff, #0f8bc5);
    }
  }

  .nav3 {
    background: linear-gradient(to right, #45efc5, #08d1a7);

    p {
      background: linear-gradient(to right, #7dede4, #10cbbc);
    }
  }

  .nav4 {
    background: linear-gradient(to right, #bbdfff, #97cdf9);

    p {
      background: linear-gradient(to right, #2b95ec, #0863af);
    }
  }

  .nav5 {
    background: linear-gradient(to right, #f7db8e, #fdb659);

    p {
      background: linear-gradient(to right, #ffddaf, #ffa530);
    }
  }

  // 活动 促销
  .promotion {
    margin-top: 120px;
    position: relative;

    > .all {
      position: absolute;
      top: 86px;
      right: 0;
      font-size: 16px;
      color: #333333;
      cursor: pointer;
    }
  }

  .goods_titles {
    margin-bottom: 60px;
    height: 60px;
    line-height: 60px;
    position: relative;
    text-align: center;
    font-size: 30px;
    font-weight: 600;

    i {
      display: inline-block;
      height: 2px;
      width: 440px;
      background: #38eec1;
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
    }

    span {
      display: block;
      width: 190px;
      background: white;
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
    }
  }

  .promotion > .goods {
    margin-bottom: 120px;
    display: flex;
    flex-wrap: wrap;
    > div {
      cursor: pointer;
      height: 240px;
      width: 20%;
      padding: 10px;
      border: 1px solid #e6e6e6;
      border-left: none;
    }

    > div:nth-of-type(1) {
      border-left: 1px solid #e6e6e6;
    }

    img {
      width: 100%;
      height: 150px;
    }

    .one {
      font-size: 14px;
      color: #000;
    }

    p {
      text-align: center;
      margin-top: 10px;
      color: #ff0000;
      font-size: 20px;

      span {
        font-size: 12px;
        color: #999999;
        text-decoration: line-through;
      }
    }
  }

  // 产品推荐
  .recommend {
    margin-bottom: 40px;
  }

  .recommend .navs {
    cursor: Default;
    margin: auto;
    width: 1000px;
    height: 42px;
    font-size: 18px;
    padding-left: 20px;
    display: flex;
    border-bottom: 1px solid #e6e6e6;

    > div {
      box-sizing: content-box;
      padding-left: 14px;
      padding-right: 14px;
      line-height: 42px;
      position: relative;
    }

    i {
      position: absolute;
      top: 14px;
      right: -1px;
      display: block;
      width: 1px;
      height: 18px;
      border-right: 1px solid #e6e6e6;
    }

    // 推荐产品 选中
    .recommend_Active {
      color: #38eec1;
      border: 1px solid #e6e6e6;

      i {
        border: none !important;
      }

      span {
        position: absolute;
        bottom: -2px;
        left: 0;
        display: block;
        width: 100%;
        height: 4px;
        background: white;
      }
    }
  }

  .recommend > .goods {
    margin-top: 40px;
    overflow: hidden;
    > div {
      cursor: Pointer;
      width: 250px;
      height: 418px;
      padding: 6px;
      text-align: left;
      border: 1px solid #e6e6e6;
      position: relative;
      float: left;
      margin-right: 50px;
      margin-bottom: 20px;
    }

    .bottom {
      width: 100%;
      position: absolute;
      // left : 10px;
      bottom: 14px;
    }

    .goodsImg {
      width: 100%;
      height: 234px;
      margin-bottom: 10px;
    }

    img {
      width: 100%;
      height: 100%;
    }

    .one {
      font-size: 24px;
      color: #f00000;
      margin-bottom: 8px;
    }

    .two {
      font-size: 16px;
      margin-bottom: 4px;
    }

    p {
      font-size: 16px;
      margin-bottom: 10px;

      span {
        color: #f00000;
      }
    }

    button {
      display: block;
      margin: auto;
      width: 208px;
      height: 32px;
      background: #f00000;
      font-size: 16px;
      border-radius: 4px;
    }
  }
}
</style>